package unamur.appmobile.ws.catalogclient;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.apache.axis2.AxisFault;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import be.fundp.info.util.logging.MyLogger;

import unamur.appmobile.ws.catalogclient.CatalogServStub.Book;
import unamur.appmobile.ws.catalogclient.CatalogServStub.GetCatalog;
import unamur.appmobile.ws.catalogclient.CatalogServStub.GetCatalogResponse;

/**
 * Web Service Client
 * Retrieve the catalog from the OnlineLibrary
 * 
 * @author gilles
 * @version 1.0 16/05/2013
 */

public class CatalogClient {

	/**
	 * logging facility
	 */
	private static final Logger logger = MyLogger.getLogger(CatalogClient.class);
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// Set up a simple configuration that logs on the console.
	     BasicConfigurator.configure();

		CatalogServStub cstub;
		try {
			cstub = new CatalogServStub();
		

		GetCatalog catalog = new GetCatalog();
		
		GetCatalogResponse catalogResponse = cstub.getCatalog(catalog);
		
		if (catalogResponse.is_returnSpecified()) {

			Book[] books = catalogResponse.get_return();
			
			// Display the catalog
			String str = "";
			for (int i=0; i<books.length; i++) {
				str += books[i].getIsbn() + " | \t" 
						+ books[i].getTitle() + " | \t" 
						+ books[i].getAuthor()+ " | \t" 
						+ books[i].getLanguage() + " | \n\n";
			}
			logger.info("Catalog is :");
			logger.info("--------------");
			logger.info("\n\nISBN	      | \tTitle	 | \tAuthor(s)	 | \tLanguage	| \n\n" + str);
		} else {
			logger.info("unable to retrieve catalog.");	
			//System.exit(-1);
		}
		
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(e);	
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(e);	
		}
	}

}
